package com.triones.api.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.EmailCodeTypeConstants;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.email.EmailValidationUtils;
import com.ruoyi.common.utils.redis.RedisUtils;
import com.triones.api.entity.MemberDO;
import com.triones.api.entity.bo.EmailChangeRecordBO;
import com.triones.api.entity.vo.CustomEmailChangeRecordVO;
import com.triones.api.mapper.EmailChangeRecordMapper;
import com.triones.api.mapper.MemberMapper;
import com.triones.api.service.EmailChangeRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 邮箱更换记录 -业务实现
 */
@Service
@RequiredArgsConstructor
public class EmailChangeRecordServiceImpl implements EmailChangeRecordService {

    private final EmailChangeRecordMapper emailChangeRecordMapper;

    private final MemberMapper memberMapper;

    @Override
    public TableDataInfo<CustomEmailChangeRecordVO> pageList(String keyword, PageQuery pageQuery) {
        QueryWrapper<Object> wrapper = Wrappers.query();
        wrapper.like(ObjectUtil.isNotEmpty(keyword),"tecr.before_replacement",keyword).or()
            .like(ObjectUtil.isNotEmpty(keyword),"tecr.after_replacement",keyword).or()
            .like(ObjectUtil.isNotEmpty(keyword),"tm.nickname",keyword);
        wrapper.orderByAsc("tecr.create_time");
        Page<CustomEmailChangeRecordVO> result = emailChangeRecordMapper.selectPageList(pageQuery.build(), wrapper);
        return TableDataInfo.build(result);
    }

    @Override
    public boolean redirect(EmailChangeRecordBO bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        MemberDO memberDO = memberMapper.selectById(loginUser.getUserId());
        if (memberDO == null) {
            throw new ServiceException("当前用户不存在");
        }
        if (!bo.getOldEmail().equals(memberDO.getEmail())) {
            throw new ServiceException("当前邮箱与原邮箱不一致");
        }
        if (EmailValidationUtils.isValidEmail(bo.getNewEmail())) {
            throw new RuntimeException("邮箱格式不正确");
        }
        String code = Convert.toStr(RedisUtils.getCacheObject(EmailCodeTypeConstants.CW_EMAIL_CHANGE_CODE + bo.getNewEmail()),"");
        if (!code.equals(bo.getCode())) {
            throw new ServiceException("验证码错误!");
        }
        memberDO.setEmail(bo.getNewEmail());
        return memberMapper.updateById(memberDO) > 0;
    }
}
